package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.common.validator.ValidatorUtils;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.cfg.entity.CfgDictitem;
import com.xhtt.modules.cfg.service.CfgDictitemService;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 数据字典管理
 */
@Api(value = "数据字典-数据字典管理", tags = "数据字典-数据字典管理")
@RestController
@RequestMapping("app/sys/cfg")
public class CfgController extends AbstractController {
    @Autowired
    private CfgDictitemService cfgDictitemService;

    /**
     * 字典表相关数据列表
     */
    @ApiOperation("字典表相关数据列表：dictId=3=码头泊位库")
    @GetMapping("/list")
    @Login
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = cfgDictitemService.queryCfgPage(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增数据字典表-明细表
     */
    @PostMapping(value = "/save")
    @ApiOperation("新增数据字典")
    @Login
    public R save(@RequestBody CfgDictitem cfgDictitem) {
        ValidatorUtils.validateEntity(cfgDictitem);
        cfgDictitemService.save(cfgDictitem);
        return R.ok();
    }

    /**
     * 删除数据字典表假删除
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除数据字典表-明细表")
    @Login
    public R delete(@RequestBody Long[] ids) {
        CfgDictitem cd = new CfgDictitem();
        cd.setStatus(-1);
        cfgDictitemService.update(cd, new QueryWrapper<CfgDictitem>().in(CfgDictitem.ID, Arrays.asList(ids)));
        return R.ok();
    }

    /**
     * 修改数据字典表-明细表
     */
    @PostMapping(value = "/update")
    @ApiOperation("修改数据字典表-明细表")
    @Login
    public R update(@RequestBody CfgDictitem cfgDictitem) {
        Integer id = cfgDictitem.getId();
        if (null == id || id < 1) {
            throw new RRException("参数有误");
        }
        ValidatorUtils.validateEntity(cfgDictitem);
        cfgDictitemService.updateById(cfgDictitem);
        return R.ok();
    }

    /**
     * 获取数据字典表-明细表
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("获取数据字典表-明细表")
    @Login
    public R info(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, cfgDictitemService.getById(id));
    }


}
